﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.AdminCP.Class;
using Telerik.Web.UI;
using MLM.Base;

namespace MLM.AdminCP.Pages
{
    public partial class LanguageMananger : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            Permission.CheckIsAdmin();
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Permission.CheckIsAdmin();
            RadLanguageGrid.ItemUpdated += new GridUpdatedEventHandler(radLanguageGrid_ItemUpdated);
            RadLanguageGrid.ItemInserted += new GridInsertedEventHandler(radLanguageGrid_ItemInserted);
            RadLanguageGrid.ItemDeleted += new GridDeletedEventHandler(radLanguageGrid_ItemDeleted);
            var sBasePath = Server.MapPath("~/AdminCP/LanguageIcon/24/");
            string[] files = System.IO.Directory.GetFiles(sBasePath, "*.png");
            List<Flag> oList = new List<Flag>();
            foreach (string s in files)
            {
                var url = System.IO.Path.GetFileName(s);
                var name = System.IO.Path.GetFileNameWithoutExtension(s);
                oList.Add(new Flag { Name = name, Url = url });
            }
            FlagRpt.DataSource = oList;
            FlagRpt.DataBind();
        }
        void radLanguageGrid_ItemDeleted(object sender, GridDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                Message.ShowError("Language " + e.Item["LanguageID"].Text + " cannot be deleted. Reason: " + e.Exception.Message);
            }
            else
            {
                Message.ShowSuccess("Language " + e.Item["LanguageID"].Text + " deleted!");
            }
        }

        void radLanguageGrid_ItemInserted(object sender, GridInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                Message.ShowError("Language cannot be inserted. Reason: " + e.Exception.Message);
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
            }
            else
            {
                Message.ShowSuccess("Language inserted!");
            }
        }

        void radLanguageGrid_ItemUpdated(object sender, GridUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.KeepInEditMode = true;
                e.ExceptionHandled = true;
                Message.ShowError("Language " + e.Item["LanguageID"].Text + " cannot be updated. Reason: " + e.Exception.Message);
            }
            else
            {
                Message.ShowSuccess("Language " + e.Item["LanguageID"].Text + " updated.");
            }
        }
    }
    public class Flag
    {
        public string Name { get; set; }
        public string Url { get; set; }
    }
}